package com.mxprojects.admin
{
	import com.mxprojects.constructors.ButtonConstructor;
	import com.mxprojects.constructors.ButtonLarge;
	import com.mxprojects.constructors.Divider;
	import com.mxprojects.constructors.InputConstructor;
	import com.mxprojects.gui.DefaultDialog;
	import com.mxprojects.gui.Styles;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.CustomEvent;
	import com.mxprojects.io.ServerHandler;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	import com.mxprojects.views.Admin;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class AdminConfiguration extends Admin
	{
		private static const DISABLED_VALUES:Array = new Array('GUEST_GROUP_ID', 'INSTALL_DIRECTORY', 
													'ABSOLUTE_INSTALL_PATH', 'MOST_ONLINE_USERS', 'ADMIN_MAIL_ON_THREAD_CHANGE');
		private static var saveButton:ButtonLarge;
		private static var dataToSend:Object;
		
		public function AdminConfiguration(){}
		
		/**
		 * Enables editing of configuration values
		 */		
		public static function display():void {
			//set the text
			setAdminTitle(Language.main(Word.CONFIGURATION));
			
			//setup the content holder
			setupHolders();
			
			//attach some text and a few input fields to the holder
			displayConfigUI();
			
			//make sure they are at the proper gap
			setLeftAndRight();
		}
		
		/**
		 * Puts to the UI on the stage 
		 */		
		private static function displayConfigUI():void {
			//take the config count from the server, split it in half and create inputs
			var configTotal:int = dataFromServer.configTotal;
			var splitCount:int = Math.floor((configTotal-DISABLED_VALUES.length)/2);
			var nextY:int = 0;
			var wideLabelLeft:int;
			var wideLabelRight:int;
			var acceptedCount:int = 0;
			
			for(var i:int = 0; i < configTotal; i++){
				//if we are at the split count then we need to reset the nextY
				if(acceptedCount == splitCount){
					nextY = 0;
				}
				
				//create an input
				var configName:String = dataFromServer["config"+i+"Name"];
				
				if(DISABLED_VALUES.indexOf(configName) == -1){
					var configValue:String = dataFromServer["config"+i+"Value"];
					var input:InputConstructor = new InputConstructor(Language.main(configName), configValue, i+1);
					input.y = nextY;
					input.name = configName;
					nextY += input.height + Config.TEXT_PADDING/2;
					
					//add it to the right side
					if(acceptedCount < splitCount){
						leftHolder.addChild(input);
						if(input.inputLabel.width > wideLabelLeft){
							wideLabelLeft = input.inputLabel.width;
						}
					}else{
						rightHolder.addChild(input);
						if(input.inputLabel.width > wideLabelRight){
							wideLabelRight = input.inputLabel.width;
						}
					}
					acceptedCount++;
				}
			}
			
			//once we are done let's loop through the inputs and make things a little more straight
			alignInputs(leftHolder, wideLabelLeft);
			alignInputs(rightHolder, wideLabelRight);
			
			//add a divider
			var divider:Divider = new Divider(1, 0xffcc00);
			Styles.setProperties(divider, ".searchDivider");
			divider.x = wideLabelLeft;
			divider.y = baseHolder.height + Config.TEXT_PADDING*2;
			divider.width = Math.round(leftHolder.width + rightHolder.width - wideLabelLeft + Config.COLUMN_PADDING);
			baseHolder.addChild(divider);
			
			//add a button to save all this data
			saveButton = new ButtonLarge(Language.main(Word.SAVE), onSaveClick);
			saveButton.x = divider.x;
			saveButton.y = Math.round(baseHolder.y + baseHolder.height + Config.TEXT_PADDING*2);
			baseHolder.addChild(saveButton);
		}
		
		private static function onSaveClick(event:MouseEvent):void {
			//take all this data and send it back to the server
			dataToSend = new Object();
			dataToSend.action = AdminActions.SAVE_CONFIG;
			
			var errors:String = '';
			
			errors += setData(leftHolder);
			if(errors == 'MISSING_DATA'){ 
				DefaultDialog.show(Language.main(Word.ERROR_REQURED_FIELDS)); 
				return;
			}
			
			errors += setData(rightHolder);
			if(errors == 'MISSING_DATA'){ 
				DefaultDialog.show(Language.main(Word.ERROR_REQURED_FIELDS)); 
				return;
			}
			
			//we have errors!
			if(errors != ''){
				DefaultDialog.show(errors);
				return;
			}
			
			var adminHandleData:AdminHandleData = new AdminHandleData(dataToSend, saveConfigSuccess, saveButton, ADMIN_SCRIPT);
		}
		
		private static function setData(holder:Sprite):String {
			var errors:String = '';
			var i:int = 0;
			var child:DisplayObject;
			var value:String;
			
			for(i; i < holder.numChildren; i++){
				child = holder.getChildAt(i);
				if(child is InputConstructor){
					//check a few things before setting the data
					value = InputConstructor(child).getValue();
					if(value == '') return 'MISSING_DATA';
					switch(child.name){
						case 'MAX_IMAGE_WIDTH':
						case 'MAX_IMAGE_HEIGHT':
							if(int(value) > 2880){
								errors += addError(child, Language.main(Word.ERROR_MAX_BITMAP_SIZE));
							}
							break;
						
						case 'ADD_AVATAR_ON_REGISTER':
						case 'POST_SUBSCRIBE_BY_DEFAULT':
							if(int(value) < 0 || int(value) > 1){
								errors += addError(child, Language.main(Word.ERROR_INVALID_ZERO_OR_ONE));
							}
							break;
					}
					dataToSend[child.name] = value;
				}
			}
			
			return errors;
		}
		
		private static function saveConfigSuccess(event:CustomEvent):void {
			DefaultDialog.show(Language.main(Word.MESSAGE_SAVE_CONFIG), true, Word.SUCCESS);
			
			//re-populate our config!
			Config.processData(event);
		}
		
		private static function addError(child:DisplayObject, errorMsg:String):String {
			return '<b>'+Language.main(child.name)+': </b>'+errorMsg+'\n\n';
		}
		
	}
}